/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.forecast.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.forecast.domain.RaceForecast;
import com.fzhucc.modules.forecast.domain.dto.ForecastDto;
import com.fzhucc.modules.forecast.domain.dto.RaceForecastDto;
import com.fzhucc.modules.forecast.service.RaceForecastService;
import com.fzhucc.modules.forecast.domain.dto.RaceForecastQueryCriteria;
import com.fzhucc.modules.wxApplet.domain.ChangeRandomDto;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-08-11
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "大赛预报名:管理")
@RequestMapping("/api/raceForecast")
public class RaceForecastController {

    private final RaceForecastService raceForecastService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('raceForecast:list')")
    public void exportRaceForecast(HttpServletResponse response, RaceForecastQueryCriteria criteria) throws IOException {
        raceForecastService.download(raceForecastService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询raceforecast")
    @ApiOperation("查询raceforecast")
    @PreAuthorize("@el.check('raceForecast:list')")
    public ResponseEntity<Object> queryRaceForecast(RaceForecastQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(raceForecastService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增raceforecast")
    @ApiOperation("新增raceforecast")
    @PreAuthorize("@el.check('raceForecast:add')")
    public ResponseEntity<Object> createRaceForecast(@Validated @RequestBody RaceForecast resources){
        return new ResponseEntity<>(raceForecastService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改raceforecast")
    @ApiOperation("修改raceforecast")
    @PreAuthorize("@el.check('raceForecast:edit')")
    public ResponseEntity<Object> updateRaceForecast(@Validated @RequestBody RaceForecast resources){
        raceForecastService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除raceforecast")
    @ApiOperation("删除raceforecast")
    @PreAuthorize("@el.check('raceForecast:del')")
    public ResponseEntity<Object> deleteRaceForecast(@RequestBody Integer[] ids) {
        raceForecastService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("预报名大赛")
    @ApiOperation("预报名大赛")
    @AnonymousPostMapping("/forecast")
    public ResponseEntity<Object> forecast(@RequestBody ForecastDto forecastDto) {
        return new ResponseEntity<>(raceForecastService.forecast(forecastDto), HttpStatus.OK);
    }
}